package com.zhiyou100.unit;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class SendMessage {
    /*
    pom.xml
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-core</artifactId>
      <version>4.0.3</version>
    </dependency>
    */

        public static String SendMessage(String phoneNum) {
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4Fgcm96j7TFEfSsCCw3H", "EuhmTV0ibZ6vPZ22HrjK8tg0F2xjqj");
            IAcsClient client = new DefaultAcsClient(profile);

            CommonRequest request = new CommonRequest();
            request.setMethod(MethodType.POST);
            request.setDomain("dysmsapi.aliyuncs.com");
            request.setVersion("2017-05-25");
            request.setAction("SendSms");
            request.putQueryParameter("RegionId", "cn-hangzhou");
            request.putQueryParameter("PhoneNumbers", phoneNum);
            request.putQueryParameter("SignName", "飞衡");
            request.putQueryParameter("TemplateCode", "SMS_184120192");
            String code = "";
            int[] codes = {0,1,2,3,4,5,6,7,8,9};
            for(int i=0;i<6;i++){
                code = code+ codes[(int) Math.floor(Math.random()*10)];
            }
            request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
            System.out.println("已发送");
            try {
                CommonResponse response = client.getCommonResponse(request);
                System.out.println(response.getData());
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
            return code;
        }

}
